package com.dayatang.android.Db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.dayatang.android.Model.Project;
import com.dayatang.android.Model.ProjectSearchLog;
import com.dayatang.android.Model.SubProject;

public class ProjectSearchServiceDb {
	
	private DBOpenHelper dbOpenHelper;

	public ProjectSearchServiceDb(Context context) {
		this.dbOpenHelper = new DBOpenHelper(context);
	}

	public void saveLog(ProjectSearchLog projectSearchLog) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		values.put("project_name", projectSearchLog.getName());
		values.put("budget", projectSearchLog.getBudget());
		values.put("output", projectSearchLog.getOutput());
		values.put("projectid", projectSearchLog.getProjectid());
		db.insert("project_search_log", null, values);
		
		db.close();
	}
	
	public List<ProjectSearchLog> findLog() {
		List<ProjectSearchLog> projectSearchLogs = new ArrayList<ProjectSearchLog>();
		
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		Cursor cursor = db.query("project_search_log", null, null, null, null, null, "id desc");
		
		while(cursor.moveToNext()) {
			
			ProjectSearchLog projectSearchLog = new ProjectSearchLog();
			
			projectSearchLog.setId(cursor.getLong(cursor.getColumnIndex("id")));
			projectSearchLog.setName(cursor.getString(cursor.getColumnIndex("project_name")));
			projectSearchLog.setOutput(cursor.getString(cursor.getColumnIndex("output")));
			projectSearchLog.setProjectid(cursor.getString(cursor.getColumnIndex("projectid")));
			projectSearchLog.setBudget(cursor.getString(cursor.getColumnIndex("budget")));
			
			projectSearchLogs.add(projectSearchLog);
		}
		cursor.close();
		
		db.close();
		return projectSearchLogs;
	}
	
	public void delLog(String id) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		db.delete("project_search_log", "projectid=?", new String[]{id});
		db.close();
	}
	
	public Project getProjectByProjectId(String projectid) {
		
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		Cursor cursor = db.query("projects", null, "projectid=?", new String[]{projectid}, null, null, null);
		
		Project project = new Project();
		
		if(cursor.moveToFirst()) {
			
			project.setName(cursor.getString(cursor.getColumnIndex("name")));
			project.setStatus(cursor.getString(cursor.getColumnIndex("status")));
			project.setStartDate(cursor.getString(cursor.getColumnIndex("start_date")));
			project.setFinishDate(cursor.getString(cursor.getColumnIndex("finish_date")));
			project.setProjectType(cursor.getString(cursor.getColumnIndex("project_type")));
			project.setArea(cursor.getString(cursor.getColumnIndex("area")));
			project.setResponsibleDivision(cursor.getString(cursor.getColumnIndex("responsible_division")));
			project.setOutput(cursor.getString(cursor.getColumnIndex("output")));
			project.setBudget(cursor.getString(cursor.getColumnIndex("budget")));
			project.setExpenditure(cursor.getString(cursor.getColumnIndex("expenditure")));
			project.setReceipt(cursor.getString(cursor.getColumnIndex("receipt")));
			project.setOwnerInfo(cursor.getString(cursor.getColumnIndex("owner_info")));
			project.setSupervisorInfo(cursor.getString(cursor.getColumnIndex("supervisor_info")));
			project.setDesignerInfo(cursor.getString(cursor.getColumnIndex("designer_info")));
			project.setProjectId(cursor.getString(cursor.getColumnIndex("projectid")));
			project.setContractNumber(cursor.getString(cursor.getColumnIndex("contract_number")));
			project.setSubProjectNum(cursor.getString(cursor.getColumnIndex("sub_project_num")));
			project.setSubProjectFinishedNum(cursor.getString(cursor.getColumnIndex("sub_project_finished_num")));
			project.setSubProjectNoFinishedNum(cursor.getString(cursor.getColumnIndex("sub_project_nofinished_num")));
			project.setWorkItemQuantity(cursor.getString(cursor.getColumnIndex("workitem_quantity")));
			project.setSpecialtyOutput(cursor.getString(cursor.getColumnIndex("specialty_output")));
			
		}
		cursor.close();
		db.close();
		
		return project;
	}
	
	public void delAllProject() {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		db.delete("projects", null, null);
		
		db.close();
	}
	
	public void saveProject(Project project) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		values.put("name", project.getName());
		values.put("status", project.getStatus());
		values.put("start_date", project.getStartDate());
		values.put("finish_date", project.getFinishDate());
		values.put("project_type", project.getProjectType());
		values.put("area", project.getArea());
		values.put("responsible_division", project.getResponsibleDivision());
		values.put("budget", project.getBudget());
		values.put("output", project.getOutput());
		values.put("expenditure", project.getExpenditure());
		values.put("receipt", project.getReceipt());
		values.put("owner_info", project.getOwnerInfo());
		values.put("supervisor_info", project.getSupervisorInfo());
		values.put("designer_info", project.getDesignerInfo());
		values.put("projectid", project.getProjectId());
		values.put("contract_number", project.getContractNumber());
		values.put("sub_project_num", project.getSubProjectNum());
		values.put("sub_project_finished_num", project.getSubProjectFinishedNum());
		values.put("sub_project_nofinished_num", project.getSubProjectNoFinishedNum());
		values.put("workitem_quantity", project.getWorkItemQuantity());
		values.put("specialty_output", project.getSpecialtyOutput());
		
		db.insert("projects", null, values);
		db.close();
	}
	
	public void saveSubProject(SubProject subProject) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		values.put("name", subProject.getName());
		values.put("start_date", subProject.getStartDate());
		values.put("finish_date", subProject.getFinishDate());
		values.put("project_type", subProject.getProjectType());
		values.put("area", subProject.getArea());
		values.put("responsible_division", subProject.getResponsibleDivision());
		values.put("budget", subProject.getBudget());
		values.put("output", subProject.getOutput());
		values.put("owner_info", subProject.getOwnerInfo());
		values.put("supervisor_info", subProject.getSupervisorInfo());
		values.put("designer_info", subProject.getDesignerInfo());
		values.put("projectid", subProject.getProjectid());
		values.put("subprojectid", subProject.getSubprojectid());
		values.put("ap_type", subProject.getApType());
		values.put("delist_code", subProject.getDelistCode());
		values.put("delisting_entrust_type", subProject.getDelistingEntrustType());
		values.put("address", subProject.getAddress());
		values.put("option_cable_finish_date", subProject.getOpticalCableFinishDate());
		values.put("transmission_open_date", subProject.getTransmissionOpenDate());
		values.put("focus", subProject.getFocus());
		values.put("progress_info", subProject.getProgressInfo());
		values.put("workitem_quantity", subProject.getWorkItemQuantity());
		values.put("specialty_output", subProject.getSpecialtyOutput());
		values.put("construction_team", subProject.getConstructionTeam());
		
		db.insert("sub_project", null, values);
		db.close();
	}
	
	public List<SubProject> findSubProject(String selection, String[] selectionArgs) {
		
		List<SubProject> subProjects = new ArrayList<SubProject>();
		
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		Cursor cursor = db.query("sub_project", null, selection, selectionArgs, null, null, null);
		
		while(cursor.moveToNext()) {
			
			SubProject subProject = new SubProject();
			
			subProject.setName(cursor.getString(cursor.getColumnIndex("name")));
			subProject.setStartDate(cursor.getString(cursor.getColumnIndex("start_date")));
			subProject.setFinishDate(cursor.getString(cursor.getColumnIndex("finish_date")));
			subProject.setProjectType(cursor.getString(cursor.getColumnIndex("project_type")));
			subProject.setArea(cursor.getString(cursor.getColumnIndex("area")));
			subProject.setResponsibleDivision(cursor.getString(cursor.getColumnIndex("responsible_division")));
			subProject.setOutput(cursor.getString(cursor.getColumnIndex("output")));
			subProject.setBudget(cursor.getString(cursor.getColumnIndex("budget")));
			subProject.setOwnerInfo(cursor.getString(cursor.getColumnIndex("owner_info")));
			subProject.setSupervisorInfo(cursor.getString(cursor.getColumnIndex("supervisor_info")));
			subProject.setDesignerInfo(cursor.getString(cursor.getColumnIndex("designer_info")));
			subProject.setProjectid(cursor.getString(cursor.getColumnIndex("projectid")));
			subProject.setSubprojectid(cursor.getString(cursor.getColumnIndex("subprojectid")));
			subProject.setApType(cursor.getString(cursor.getColumnIndex("ap_type")));
			subProject.setDelistCode(cursor.getString(cursor.getColumnIndex("delist_code")));
			subProject.setDelistingEntrustType(cursor.getString(cursor.getColumnIndex("delisting_entrust_type")));
			subProject.setAddress(cursor.getString(cursor.getColumnIndex("address")));
			subProject.setOpticalCableFinishDate(cursor.getString(cursor.getColumnIndex("option_cable_finish_date")));
			subProject.setTransmissionOpenDate(cursor.getString(cursor.getColumnIndex("transmission_open_date")));
			subProject.setFocus(cursor.getString(cursor.getColumnIndex("focus")));
			subProject.setProgressInfo(cursor.getString(cursor.getColumnIndex("progress_info")));
			subProject.setWorkItemQuantity(cursor.getString(cursor.getColumnIndex("workitem_quantity")));
			subProject.setSpecialtyOutput(cursor.getString(cursor.getColumnIndex("specialty_output")));
			subProject.setConstructionTeam(cursor.getString(cursor.getColumnIndex("construction_team")));
			
			subProjects.add(subProject);
		}
		cursor.close();
		db.close();
		
		return subProjects;
	}
	
	public SubProject getSubProjectBySubProjectId(String subProjectId) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		Cursor cursor = db.query("sub_project", null, "subprojectid=?", new String[]{subProjectId}, null, null, null);
		
		SubProject subProject = new SubProject();
		
		if(cursor.moveToFirst()) {
			
			subProject.setName(cursor.getString(cursor.getColumnIndex("name")));
			subProject.setStartDate(cursor.getString(cursor.getColumnIndex("start_date")));
			subProject.setFinishDate(cursor.getString(cursor.getColumnIndex("finish_date")));
			subProject.setProjectType(cursor.getString(cursor.getColumnIndex("project_type")));
			subProject.setArea(cursor.getString(cursor.getColumnIndex("area")));
			subProject.setResponsibleDivision(cursor.getString(cursor.getColumnIndex("responsible_division")));
			subProject.setOutput(cursor.getString(cursor.getColumnIndex("output")));
			subProject.setBudget(cursor.getString(cursor.getColumnIndex("budget")));
			subProject.setOwnerInfo(cursor.getString(cursor.getColumnIndex("owner_info")));
			subProject.setSupervisorInfo(cursor.getString(cursor.getColumnIndex("supervisor_info")));
			subProject.setDesignerInfo(cursor.getString(cursor.getColumnIndex("designer_info")));
			subProject.setProjectid(cursor.getString(cursor.getColumnIndex("projectid")));
			subProject.setSubprojectid(cursor.getString(cursor.getColumnIndex("subprojectid")));
			subProject.setApType(cursor.getString(cursor.getColumnIndex("ap_type")));
			subProject.setDelistCode(cursor.getString(cursor.getColumnIndex("delist_code")));
			subProject.setDelistingEntrustType(cursor.getString(cursor.getColumnIndex("delisting_entrust_type")));
			subProject.setAddress(cursor.getString(cursor.getColumnIndex("address")));
			subProject.setOpticalCableFinishDate(cursor.getString(cursor.getColumnIndex("option_cable_finish_date")));
			subProject.setTransmissionOpenDate(cursor.getString(cursor.getColumnIndex("transmission_open_date")));
			subProject.setFocus(cursor.getString(cursor.getColumnIndex("focus")));
			subProject.setProgressInfo(cursor.getString(cursor.getColumnIndex("progress_info")));
			subProject.setWorkItemQuantity(cursor.getString(cursor.getColumnIndex("workitem_quantity")));
			subProject.setSpecialtyOutput(cursor.getString(cursor.getColumnIndex("specialty_output")));
			subProject.setConstructionTeam(cursor.getString(cursor.getColumnIndex("construction_team")));
			
		}
		cursor.close();
		db.close();
		
		return subProject;
	}
	
	public void delAllSubProject() {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		db.delete("sub_project", null, null);
		
		db.close();
	}
}
